/*
 * 类名称:AdminDictServiceImpl.java
 * 包名称:com.iotechn.unimall.admin.api.dict
 *
 * 修改履历:
 *     日期                       修正者        主要内容
 *     2020-06-24 14:12:06        lin     初版做成
 *
 * Copyright (c) 2019-2019 浵浵科技
 */
package com.iotechn.unimall.admin.api.dict;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.mapper.Wrapper;
import com.iotechn.unimall.biz.service.dict.DictBizService;
import com.iotechn.unimall.core.exception.AdminServiceException;
import com.iotechn.unimall.core.exception.ExceptionDefinition;
import com.iotechn.unimall.core.exception.ServiceException;
import com.iotechn.unimall.data.domain.DictDO;
import com.iotechn.unimall.data.mapper.DictMapper;
import com.iotechn.unimall.data.model.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.util.StringUtils;
import org.apache.ibatis.session.RowBounds;

import java.util.Date;
import java.util.Arrays;
import java.util.List;
import java.util.Map;

/**
 * Admin系统字典表ServiceImpl实现类
 *
 * @author lin
 * @date 2020-06-24 14:12:06
 */
@Service
public class AdminDictServiceImpl implements AdminDictService {

    @Autowired
    private DictMapper dictMapper;
    @Autowired
    private DictBizService dictBizService;

    @Override
    public List<DictDO> dicListByDatatype(String datatype) throws ServiceException {
        if( StringUtils.isEmpty(datatype) ){
            throw new AdminServiceException(" 数据类别不能为空",ExceptionDefinition.PARAM_IS_NULL_CODE);
        }
        return dictBizService.dicListByDatatype(datatype);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Integer create(Long adminId, DictDO dictDO) throws ServiceException{
        if (dictDO == null){
            throw new AdminServiceException(ExceptionDefinition.REQUEST_CORE_PARAM_CHECK_FAILED);
        }
           if( StringUtils.isEmpty(dictDO.getDatatype()) ){
            throw new AdminServiceException(" 数据类别 不能为空",ExceptionDefinition.PARAM_IS_NULL_CODE);
        }
         if( StringUtils.isEmpty(dictDO.getDatacode()) ){
            throw new AdminServiceException(" 数据编码 不能为空",ExceptionDefinition.PARAM_IS_NULL_CODE);
        }
         if( StringUtils.isEmpty(dictDO.getDatavalue()) ){
            throw new AdminServiceException(" 数据名称/值 不能为空",ExceptionDefinition.PARAM_IS_NULL_CODE);
        }
      
        Date now = new Date();
        dictDO.setId(null);
        dictDO.setGmtCreate(now);
        dictDO.setGmtUpdate(now);

        dictBizService.clearDictCache(dictDO.getDatatype());
        return dictMapper.insert(dictDO);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean delete(Long adminId,Long id) throws ServiceException{


        DictDO dictDO = dictMapper.selectById(id);
        dictBizService.clearDictCache(dictDO.getDatatype());
        return dictMapper.delete(new EntityWrapper<DictDO>()
                .eq("id", id)) > 0;
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public Boolean update(Long adminId,DictDO dictDO) throws ServiceException{
        if (dictDO == null){
            throw new AdminServiceException(ExceptionDefinition.REQUEST_CORE_PARAM_CHECK_FAILED);
        }
                 if( StringUtils.isEmpty(dictDO.getDatatype()) ){
                throw new AdminServiceException(" 数据类别,首字母大写 不能为空",ExceptionDefinition.PARAM_IS_NULL_CODE);
            }
              if( StringUtils.isEmpty(dictDO.getDatacode()) ){
                throw new AdminServiceException(" 数据编码 不能为空",ExceptionDefinition.PARAM_IS_NULL_CODE);
            }
              if( StringUtils.isEmpty(dictDO.getDatavalue()) ){
                throw new AdminServiceException(" 数据名称/值 不能为空",ExceptionDefinition.PARAM_IS_NULL_CODE);
            }
          
        Date now = new Date();
        dictDO.setGmtUpdate(now);
        dictBizService.clearDictCache(dictDO.getDatatype());
        return dictMapper.updateById(dictDO) > 0;
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public DictDO info(Long adminId,Long id) throws ServiceException{
        if(id == null){
            throw new AdminServiceException(ExceptionDefinition.REQUEST_CORE_PARAM_CHECK_FAILED);
        }
        return dictMapper.selectById(id);
    }


    @Override
    @Transactional(rollbackFor = Exception.class)
    public Page<DictDO> list(Long adminId,
        Long id,
        String datadesc,
        String datatype,
        String datacode,
         Integer page,
         Integer limit) throws ServiceException{

        Wrapper<DictDO> wrapper = new EntityWrapper<DictDO>();
        wrapper.orderBy("dataType", false);
        wrapper.orderBy("sortNo", true);

            if( !StringUtils.isEmpty(datatype) ){
            wrapper.like("dataType", datatype);
        }
        if( !StringUtils.isEmpty(datacode) ){
            wrapper.like("datacode", datacode);
        }
        if( !StringUtils.isEmpty(datadesc) ){
            wrapper.like("datadesc", datadesc);
        }
                                                            
        List<DictDO> DictDOs = dictMapper.selectPage(new RowBounds((page - 1) * limit, limit), wrapper);
        Integer count = dictMapper.selectCount(wrapper);
        return new Page<DictDO>(DictDOs, page, limit, count);
    }
}